<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="手机" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机"></el-input>
    </el-form-item>
    <el-form-item label="头像地址" prop="headUrl">
      <el-input v-model="dataForm.headUrl" placeholder="头像地址"></el-input>
    </el-form-item>
    <el-form-item label="部门id" prop="deptId">
      <el-input v-model="dataForm.deptId" placeholder="部门id"></el-input>
    </el-form-item>
    <el-form-item label="岗位id" prop="postId">
      <el-input v-model="dataForm.postId" placeholder="岗位id"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="description">
      <el-input v-model="dataForm.description" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="状态（1：正常 0：停用）" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态（1：正常 0：停用）"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="删除标记（0:不可用 1:可用）" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="删除标记（0:不可用 1:可用）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          username: '',
          password: '',
          name: '',
          phone: '',
          headUrl: '',
          deptId: '',
          postId: '',
          description: '',
          status: '',
          createTime: '',
          updateTime: '',
          isDeleted: ''
        },
        dataRule: {
          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机不能为空', trigger: 'blur' }
          ],
          headUrl: [
            { required: true, message: '头像地址不能为空', trigger: 'blur' }
          ],
          deptId: [
            { required: true, message: '部门id不能为空', trigger: 'blur' }
          ],
          postId: [
            { required: true, message: '岗位id不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态（1：正常 0：停用）不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '删除标记（0:不可用 1:可用）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/system/sysuser/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.dataForm.username = data.sysUser.username
                this.dataForm.password = data.sysUser.password
                this.dataForm.name = data.sysUser.name
                this.dataForm.phone = data.sysUser.phone
                this.dataForm.headUrl = data.sysUser.headUrl
                this.dataForm.deptId = data.sysUser.deptId
                this.dataForm.postId = data.sysUser.postId
                this.dataForm.description = data.sysUser.description
                this.dataForm.status = data.sysUser.status
                this.dataForm.createTime = data.sysUser.createTime
                this.dataForm.updateTime = data.sysUser.updateTime
                this.dataForm.isDeleted = data.sysUser.isDeleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/system/sysuser/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'username': this.dataForm.username,
                'password': this.dataForm.password,
                'name': this.dataForm.name,
                'phone': this.dataForm.phone,
                'headUrl': this.dataForm.headUrl,
                'deptId': this.dataForm.deptId,
                'postId': this.dataForm.postId,
                'description': this.dataForm.description,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'isDeleted': this.dataForm.isDeleted
              })
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            }).catch(e=>{
              this.visible = false
              this.$emit('refreshDataList')

              console.log(e);
              
            })
          }
        })
      }
    }
  }
</script>
